//
//  HPTalk.m
//
//  Created by 啊  on 16/4/24
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "HPTalk.h"
//#import "HPExpert.h"


NSString *const kHPTalkTRemark = @"tRemark";
NSString *const kHPTalkTVoiceLength = @"tVoiceLength";
NSString *const kHPTalkTTitle = @"tTitle";
NSString *const kHPTalkTSetDateTime = @"tSetDateTime";
NSString *const kHPTalkTUseDate = @"tUseDate";
NSString *const kHPTalkTTags = @"tTags";
NSString *const kHPTalkCreateUser = @"createUser";
NSString *const kHPTalkTVoiceUrl = @"tVoiceUrl";
NSString *const kHPTalkExpert = @"Expert";
NSString *const kHPTalkCreateTime = @"createTime";
NSString *const kHPTalkTSetUser = @"tSetUser";
NSString *const kHPTalkTId = @"tId";
NSString *const kHPTalkEId = @"eId";


@interface HPTalk ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation HPTalk

@synthesize tRemark = _tRemark;
@synthesize tVoiceLength = _tVoiceLength;
@synthesize tTitle = _tTitle;
@synthesize tSetDateTime = _tSetDateTime;
@synthesize tUseDate = _tUseDate;
@synthesize tTags = _tTags;
@synthesize createUser = _createUser;
@synthesize tVoiceUrl = _tVoiceUrl;
@synthesize expert = _expert;
@synthesize createTime = _createTime;
@synthesize tSetUser = _tSetUser;
@synthesize tId = _tId;
@synthesize eId = _eId;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.tRemark = [self objectOrNilForKey:kHPTalkTRemark fromDictionary:dict];
            self.tVoiceLength = [[self objectOrNilForKey:kHPTalkTVoiceLength fromDictionary:dict] doubleValue];
            self.tTitle = [self objectOrNilForKey:kHPTalkTTitle fromDictionary:dict];
            self.tSetDateTime = [self objectOrNilForKey:kHPTalkTSetDateTime fromDictionary:dict];
            self.tUseDate = [self objectOrNilForKey:kHPTalkTUseDate fromDictionary:dict];
            self.tTags = [self objectOrNilForKey:kHPTalkTTags fromDictionary:dict];
            self.createUser = [self objectOrNilForKey:kHPTalkCreateUser fromDictionary:dict];
            self.tVoiceUrl = [self objectOrNilForKey:kHPTalkTVoiceUrl fromDictionary:dict];
            self.expert = [HPExpert modelObjectWithDictionary:[dict objectForKey:kHPTalkExpert]];
            self.createTime = [self objectOrNilForKey:kHPTalkCreateTime fromDictionary:dict];
            self.tSetUser = [self objectOrNilForKey:kHPTalkTSetUser fromDictionary:dict];
            self.tId = [[self objectOrNilForKey:kHPTalkTId fromDictionary:dict] doubleValue];
            self.eId = [[self objectOrNilForKey:kHPTalkEId fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.tRemark forKey:kHPTalkTRemark];
    [mutableDict setValue:[NSNumber numberWithDouble:self.tVoiceLength] forKey:kHPTalkTVoiceLength];
    [mutableDict setValue:self.tTitle forKey:kHPTalkTTitle];
    [mutableDict setValue:self.tSetDateTime forKey:kHPTalkTSetDateTime];
    [mutableDict setValue:self.tUseDate forKey:kHPTalkTUseDate];
    [mutableDict setValue:self.tTags forKey:kHPTalkTTags];
    [mutableDict setValue:self.createUser forKey:kHPTalkCreateUser];
    [mutableDict setValue:self.tVoiceUrl forKey:kHPTalkTVoiceUrl];
    [mutableDict setValue:[self.expert dictionaryRepresentation] forKey:kHPTalkExpert];
    [mutableDict setValue:self.createTime forKey:kHPTalkCreateTime];
    [mutableDict setValue:self.tSetUser forKey:kHPTalkTSetUser];
    [mutableDict setValue:[NSNumber numberWithDouble:self.tId] forKey:kHPTalkTId];
    [mutableDict setValue:[NSNumber numberWithDouble:self.eId] forKey:kHPTalkEId];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.tRemark = [aDecoder decodeObjectForKey:kHPTalkTRemark];
    self.tVoiceLength = [aDecoder decodeDoubleForKey:kHPTalkTVoiceLength];
    self.tTitle = [aDecoder decodeObjectForKey:kHPTalkTTitle];
    self.tSetDateTime = [aDecoder decodeObjectForKey:kHPTalkTSetDateTime];
    self.tUseDate = [aDecoder decodeObjectForKey:kHPTalkTUseDate];
    self.tTags = [aDecoder decodeObjectForKey:kHPTalkTTags];
    self.createUser = [aDecoder decodeObjectForKey:kHPTalkCreateUser];
    self.tVoiceUrl = [aDecoder decodeObjectForKey:kHPTalkTVoiceUrl];
    self.expert = [aDecoder decodeObjectForKey:kHPTalkExpert];
    self.createTime = [aDecoder decodeObjectForKey:kHPTalkCreateTime];
    self.tSetUser = [aDecoder decodeObjectForKey:kHPTalkTSetUser];
    self.tId = [aDecoder decodeDoubleForKey:kHPTalkTId];
    self.eId = [aDecoder decodeDoubleForKey:kHPTalkEId];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_tRemark forKey:kHPTalkTRemark];
    [aCoder encodeDouble:_tVoiceLength forKey:kHPTalkTVoiceLength];
    [aCoder encodeObject:_tTitle forKey:kHPTalkTTitle];
    [aCoder encodeObject:_tSetDateTime forKey:kHPTalkTSetDateTime];
    [aCoder encodeObject:_tUseDate forKey:kHPTalkTUseDate];
    [aCoder encodeObject:_tTags forKey:kHPTalkTTags];
    [aCoder encodeObject:_createUser forKey:kHPTalkCreateUser];
    [aCoder encodeObject:_tVoiceUrl forKey:kHPTalkTVoiceUrl];
    [aCoder encodeObject:_expert forKey:kHPTalkExpert];
    [aCoder encodeObject:_createTime forKey:kHPTalkCreateTime];
    [aCoder encodeObject:_tSetUser forKey:kHPTalkTSetUser];
    [aCoder encodeDouble:_tId forKey:kHPTalkTId];
    [aCoder encodeDouble:_eId forKey:kHPTalkEId];
}

- (id)copyWithZone:(NSZone *)zone
{
    HPTalk *copy = [[HPTalk alloc] init];
    
    if (copy) {

        copy.tRemark = [self.tRemark copyWithZone:zone];
        copy.tVoiceLength = self.tVoiceLength;
        copy.tTitle = [self.tTitle copyWithZone:zone];
        copy.tSetDateTime = [self.tSetDateTime copyWithZone:zone];
        copy.tUseDate = [self.tUseDate copyWithZone:zone];
        copy.tTags = [self.tTags copyWithZone:zone];
        copy.createUser = [self.createUser copyWithZone:zone];
        copy.tVoiceUrl = [self.tVoiceUrl copyWithZone:zone];
        copy.expert = [self.expert copyWithZone:zone];
        copy.createTime = [self.createTime copyWithZone:zone];
        copy.tSetUser = [self.tSetUser copyWithZone:zone];
        copy.tId = self.tId;
        copy.eId = self.eId;
    }
    
    return copy;
}


@end
